#include "w256qxxManagement.h"
#include "w25q256qspiBsp.h"
#include "stdio.h"



#define FLASH_BOOT_Address     		0x00000400

#define FLASH_PARAMETER_Address     0x00000800

#define FLASH_CONFIG_Address     	0x00000C00

#define FLASH_LOG_Address     		0x00080000

#define FLASH_STACK_Address     	0x007F0000

#define SECTOR_SIZE					4 * 1024

#define W25Q256_DEBUG_MSG			1


uint8_t w25q256BootMsgWrite(uint8_t* buffer, int32_t dataLen)
{
	uint8_t rtl = QSPI_ERROR;
	uint8_t cmpResult;	
	uint8_t readBuff[SECTOR_SIZE];
	uint32_t falshAddr = FLASH_BOOT_Address;
	
	if(buffer == NULL)
	{
		return rtl;
	}
	if((dataLen > SECTOR_SIZE) || (dataLen < 0))
	{
		return rtl;
	}
	if(BSP_QSPI_Erase_Block(falshAddr) != QSPI_OK)
	{
		return rtl;
	}
	if(BSP_QSPI_Write(buffer, falshAddr, dataLen) != QSPI_OK)
	{
		return rtl;
	}	
	//check if the written data is correct.  
	if(BSP_QSPI_Read(readBuff, falshAddr, dataLen) != QSPI_OK)
	{
		return rtl;
	}		
	
	cmpResult = qspiBuffercmp(buffer, readBuff, dataLen); 
	
	if( PASSED != cmpResult )
	{
		#if defined (W25Q256_DEBUG_MSG)
		printf("Config parameter write data filed\n");
		#endif
		return rtl;
	}
	
	#if defined (W25Q256_DEBUG_MSG)
	printf("Config parameter write data: %s :\n", buffer);	
	#endif
	return QSPI_OK;
}


uint8_t w25q256BootMsgRead(uint8_t* buffer, int32_t dataLen)
{
	uint8_t rtl;
	uint32_t falshAddr = FLASH_BOOT_Address;

	if(dataLen > SECTOR_SIZE)
	{
		return rtl;
	}
	if(dataLen < 0)
	{
		return rtl;
	}
	rtl = BSP_QSPI_Read(buffer, falshAddr, dataLen);
	#if defined (W25Q256_DEBUG_MSG)
	printf("Boot parameter  read data :%s\n", buffer);
	#endif
	return rtl;
}
	

uint8_t w25q256ConfigParameterWrite(uint8_t* buffer, int32_t dataLen)
{
	uint8_t rtl = QSPI_ERROR;
	uint8_t cmpResult;	
	uint8_t readBuff[SECTOR_SIZE];
	uint32_t falshAddr = FLASH_PARAMETER_Address;
	
	if(buffer == NULL)
	{
		return rtl;
	}
	if((dataLen > SECTOR_SIZE) || (dataLen < 0))
	{
		return rtl;
	}
	if(BSP_QSPI_Erase_Block(falshAddr) != QSPI_OK)
	{
		return rtl;
	}
	if(BSP_QSPI_Write(buffer, falshAddr, dataLen) != QSPI_OK)
	{
		return rtl;
	}	
	//check if the written data is correct.  
	if(BSP_QSPI_Read(readBuff, falshAddr, dataLen) != QSPI_OK)
	{
		return rtl;
	}		
	
	cmpResult = qspiBuffercmp(buffer, readBuff, dataLen); 
	
	if( PASSED != cmpResult )
	{
		#if defined (W25Q256_DEBUG_MSG)
		printf("Config parameter write data filed\n");
		#endif
		return rtl;
	}
	
	#if defined (W25Q256_DEBUG_MSG)
	printf("Config parameter write data: %s :\n", buffer);	
	#endif
	return QSPI_OK;
}


uint8_t w25q256ConfigParameterRead(uint8_t* buffer, int32_t dataLen)
{
	uint8_t rtl;
	uint32_t falshAddr = FLASH_PARAMETER_Address;

	if(dataLen > SECTOR_SIZE)
	{
		return rtl;
	}
	if(dataLen < 0)
	{
		return rtl;
	}
	rtl = BSP_QSPI_Read(buffer, falshAddr, dataLen);
	#if defined (W25Q256_DEBUG_MSG)
	printf("Config parameter read data :%s\n", buffer);
	#endif
	return rtl;
}
	


void w25q256FlashTestPtr(void)
{
	uint8_t bufTest[] = {"boot data test."};
	uint8_t ReadBuf[20];
	w25q256BootMsgWrite(bufTest,sizeof(bufTest));
	w25q256BootMsgRead(ReadBuf,sizeof(bufTest));
}




